﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Infrastructure.Crosscutting.Authorize
{
    public class MvcControllerService : IMvcControllerService
    {
        Domain.Uow.IRepository<Domain.Entity.MvcController>
            controllerRepository;

        public MvcControllerService(Domain.Uow.IRepository<Domain.Entity.MvcController> controller)
        {
            if (controller == null)
            {
                throw new ArgumentNullException("controllerRepository is null");
            }
            controllerRepository = controller;
        }

        public Domain.Entity.MvcController Add(Domain.Entity.MvcController controller, Domain.Entity.User operUser)
        {
            try
            {
                controllerRepository.Add(controller);
                controllerRepository.Uow.Commit();
            }
            catch (Exception ex)
            {
            }
            return controller;
        }

        public bool Delete(Domain.Entity.MvcController controller, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                controller.State = false;
                controllerRepository.Modify(controller);
                controllerRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool DeleteTrue(Domain.Entity.MvcController controller, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                controllerRepository.Delete(controller);
                controllerRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool Delete(IList<int> idList, Domain.Entity.User operUser)
        {
            var res = false;
            var delete = controllerRepository.GetList(e => idList.Contains(e.Id));
            foreach (var item in delete)
            {
                item.State = false;
                controllerRepository.Modify(item);
            }
            try
            {
                controllerRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool DeleteTrue(IList<int> idList, Domain.Entity.User operUser)
        {
            var res = false;
            var delete = controllerRepository.GetList(e => idList.Contains(e.Id));
            foreach (var item in delete)
            {
                controllerRepository.Delete(item);
            }
            try
            {
                controllerRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool Modify(Domain.Entity.MvcController controller, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                controllerRepository.Modify(controller);
                controllerRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public Domain.Entity.MvcController Get(int id)
        {
            var data = controllerRepository.Get(id);
            return data;
        }

        public IQueryable<Domain.Entity.MvcController> GetList()
        {
            var res = controllerRepository.GetList().AsQueryable();
            return res;
        }
    }
}
